#coding=utf-8

import os
import sys
import shutil
import datetime

from clitool.core.task.cocos.CocosBaseTask import CocosBaseTask


class CocosBuildTask(CocosBaseTask):

    def __init__(self, options):
        super(CocosBuildTask, self).__init__(options)

    def __start(self):
        pass

    def __finish(self, error):
        pass

    def __run(self):
        #creator = self.get_config('CocosCreator')

        name = self.get_option('name')  # 项目名称
        mode = self.get_option('mode')  # 构建debug模式
        plat = self.get_option('platform') #构建平台

        # 构建
        self.___build(name, plat, mode)

        self.___trimEngine(name, plat, mode)

        

    def ___build(self, name, platform, mode):
        print('==== %s, %s, %s' % (name, platform, mode))

        creator = self.get_config('CocosCreator')

        proj_path = self.get_config('project.path', name)

        args = 'platform=%s;'%platform

        publish_common = self.get_config('publish.%s.common' % (platform), name)
        for key, value in publish_common.items():
            args = args + ('%s=%s;' % (key, value))

        publish_mode = self.get_config('publish.%s.%s' % (platform, mode), name)
        for key, value in publish_mode.items():
            args = args + ('%s=%s;' % (key, value))

        cmd = '%s --path %s --build "%s"' % (creator, proj_path, args)

        print(cmd)

        os.system(cmd)

    def ___trimEngine(self, name, platform, mode):
        buildPath = self.get_config('publish.%s.%s.buildPath' % (platform, mode), name)

        engineDir = os.path.join(buildPath, platform, 'engine')

        print(engineDir)

        self.startProcess(10)

        os.remove(os.path.join(buildPath, platform, 'game.js'))
        os.rename(os.path.join(buildPath, platform, 'game-backup.js'), os.path.join(buildPath, platform, 'game.js'))

        self.updateProcess(1, 'trim game.js')

        os.remove(os.path.join(buildPath, platform, 'main.js'))
        os.rename(os.path.join(buildPath, platform, 'main-backup.js'), os.path.join(buildPath, platform, 'main.js'))

        self.updateProcess(2, 'trim main.js')

        if mode == 'debug':
            os.remove(os.path.join(buildPath, platform, 'engine-loader.js'))
            os.rename(os.path.join(buildPath, platform, 'engine-loader-debug.js'), os.path.join(buildPath, platform, 'engine-loader.js'))
        else:
            os.remove(os.path.join(buildPath, platform, 'engine-loader-debug.js'))

        self.updateProcess(3, 'trim engine-loader.js')

        shutil.move(os.path.join(buildPath, platform, 'src'), engineDir)
        self.updateProcess(4, 'trim folder: src')

        shutil.move(os.path.join(buildPath, platform, 'cocos'), engineDir)
        self.updateProcess(5, 'trim folder: cocos')

        shutil.copytree(os.path.join(buildPath, platform, 'assets'), os.path.join(engineDir, 'assets'))
        self.updateProcess(6, 'trim folder: assets')

        shutil.move(os.path.join(buildPath, platform, 'main.js'), engineDir)
        self.updateProcess(7, 'move: main.js')

        shutil.move(os.path.join(buildPath, platform, 'ccRequire.js'), engineDir)
        self.updateProcess(8, 'move: ccRequire.js')

        if mode == 'debug':
            shutil.move(os.path.join(buildPath, platform, 'adapter.js'), engineDir)
        else:
            shutil.move(os.path.join(buildPath, platform, 'adapter-min.js'), engineDir)

        self.updateProcess(9, 'move: adapter.js')

        if os.path.exists(os.path.join(buildPath, 'remote')):
            cur_time = datetime.datetime.now()
            os.rename(os.path.join(buildPath, 'remote'), os.path.join(buildPath, 'remote%d%d%d%d%d%d' %(cur_time.year, cur_time.month, cur_time.day, cur_time.hour, cur_time.minute, cur_time.second)))

        shutil.move(os.path.join(buildPath, platform, 'remote'), buildPath)

        self.updateProcess(10, 'move: remote')

        self.finishProcess() 


    def run(self):
        self.__start()
        try:
            self.__run()
            self.__finish(0)
        except Exception:
            self.__finish(-1)
